/*
 * @Author: coderzi
 * @Description:签到福利
 */
import { Component, OnInit, Input } from '@angular/core';
import { UserService } from 'src/app/service/user.service';
import { BaseService } from 'src/app/service/base.service';
import { UtilsService } from 'src/app/service/utils.service';
import { YearawardService } from '../../yearaward.service';
import { WELFARE_SIGN_MAP } from '../index.map';
import type { Subscription } from 'rxjs';

@Component({
  selector: 'app-welfare-sign',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss'],
})
export class WelfareSignComponent implements OnInit {
  @Input() timeConfig: any[] = [];
  /* ___ _ ___ */
  public deployUrl: string; /* 资源地址前缀 */
  private reshing: Subscription = null; /* 订阅刷新 */
  public layoutMap = WELFARE_SIGN_MAP;
  /* ___ _ ___ */
  public isPC = false; /* 是否pc */
  public isAndroid = false; /* 是否安卓 */
  public onRefrash = false; /* 刷新中 */
  public pageSize = 30; /* 每页条数 */
  public tabs: number[] = [1, 2, 3]; /* tabs */
  /* ___ _ ___ */
  public detail: EndYear.Welfare.ISignDetail = {
    todaySignIn: false /* 今日是否签到 */,
    signInDays: 0 /* 总签到天数 */,
  };

  constructor(
    private userService: UserService,
    private baseService: BaseService,
    private utilsService: UtilsService,
    private yearawardService: YearawardService
  ) {
    console.log('12=>', this.layoutMap);
  }

  ngOnInit(): void {
    // 获取
    this.deployUrl = this.userService.getDeployUrl();
    this.isPC = this.userService.getIsPc();
    this.isAndroid = this.utilsService.getBrowserVersion();
    // 请求数据
    this.init();

    // 组件刷新通信
    this.reshing = this.utilsService.MsgSub.subscribe((res: any) => {
      this.init();
    });
  }

  ngOnDestroy(): void {
    /* 取消订阅 */
    if (this.reshing) this.reshing.unsubscribe();
  }

  /**
   * @description: 初始化请求列表
   */
  // 获取签到详情
  init() {
    this.onRefrash = true;

    this.yearawardService.getSignDetail(
      ({ code, data, message }) => {
        if (code === 200) {
          const { todaySignIn, signInDays } = data;
          this.detail.todaySignIn = todaySignIn;
          this.detail.signInDays = signInDays;
          this.onRefrash = false;
        } else {
          this.baseService.showToast(message, 1500);
        }
      },
      (err) => {
        this.onRefrash = false;
      }
    );
  }

  /**
   * @description: 签到
   */
  sign() {
    this.yearawardService.sign(
      ({ code, message }) => {
        if (code === 200) {
          this.baseService.showToast('签到成功', 1500);
          this.init();
        } else {
          this.baseService.showToast(message, 1500);
        }
      },
      () => {}
    );
  }
}
